
package AddressBook.Model;

import AddressBook.Model.Exception.NoSuchKeyFoundHashMapException;
import AddressBook.Model.Exception.KeyAlreadyUsedHashMapException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The Class ContactFamilyModel provides basic functions to manage a group of contacts
 */
public class Group {
    /**
     * This field stores all the contact belonging to this family
     */
    private HashMap<String, Contact> itemHashMap;

    /**
     * This field store the name of the family
     */
    private String name;

    /**
     * This constructor initialize the family with the specified name
     *
     * @param name The name of the family
     */
    public Group(String name) {
        this.name = name;
    }


    /**
     * This method returns the name of the family
     *
     * @return The name of the family
     */
    public String getName() {
        return name;
    }

    /**
     * This method returns an array containing all the contact of the family
     *
     * @return An array of all the item
     * @throws NullPointerException If the hashmap is uninstantiated
     */
    public Contact[] getItemArray() throws NullPointerException {
        if (itemHashMap != null) {
            Contact[] i = new Contact[itemHashMap.size()];
            return itemHashMap.values().toArray(i);
        }
        throw new NullPointerException("Uninstantiated HashMap<Contact>");
    }

    /**
     * This method returns the item associated to the specified phone
     *
     * @param phone The phone of the contact
     * @return The item associated to the phone
     * @throws AddressBook.Model.Exception.NoSuchKeyFoundHashMapException
     *                              If no contact was found
     * @throws NullPointerException If the hashmap is uninstantiated
     */
    public Contact getItem(String phone) throws NoSuchKeyFoundHashMapException, NullPointerException {
        if (itemHashMap != null) {
            if (itemHashMap.containsKey(phone)) return itemHashMap.get(phone);
            else throw new NoSuchKeyFoundHashMapException("No contact found");
        }
        throw new NullPointerException("Uninstantiated HashMap<Contact>");

    }

    /**
     * This method returns true if the specified item's code is already add in the family, false otherwise
     *
     * @param phone The code to check
     * @return True if the code is already added
     * @throws NullPointerException If the hashmap is uninstantiated
     */
    public boolean containsItem(String phone) throws NullPointerException {
        if (itemHashMap != null)
            return itemHashMap.containsKey(phone);

        throw new NullPointerException("Uninstantiated HashMap<Contact>");
    }

    /**
     * This method returns a representation of the family
     *
     * @return A string representation
     */
    @Override
    public String toString() {
        return name;
    }

    /**
     * This method allows to add an contact to the family
     *
     * @param contact The contact to add
     * @throws KeyAlreadyUsedHashMapException If an contact with the same was already add
     */
    public void addContact(Contact contact) throws KeyAlreadyUsedHashMapException {
        if (itemHashMap == null) itemHashMap = new HashMap<String, Contact>();
        if (!itemHashMap.containsKey(contact.getPersonalPhone())) {
            itemHashMap.put(contact.getPersonalPhone(), contact);
        } else throw new KeyAlreadyUsedHashMapException("Contact already added");

    }

    /**
     * This method allows to remove an contact of the family
     *
     * @param contact The contact to remove
     * @throws AddressBook.Model.Exception.NoSuchKeyFoundHashMapException
     *                              If no contact was found
     * @throws NullPointerException If the hashmap is uninstantiated
     */
    public void removeContact(Contact contact) throws NoSuchKeyFoundHashMapException, NullPointerException {
        if (itemHashMap != null) {
            if (itemHashMap.containsKey(contact.getPersonalPhone())) {
                itemHashMap.remove(contact.getPersonalPhone());
            } else throw new NoSuchKeyFoundHashMapException("No Contact found");
        } else throw new NullPointerException("Uninstantiated HashMap<Contact>");
    }

}
